package com.lbs.jiepiaisi.manager;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;

import com.alibaba.fastjson.JSON;
import com.lbs.jiepiaisi.bean.JvHeCityBean;
import com.lbs.jiepiaisi.style.citylist.utils.CityListLoader;
import com.lbs.jiepiaisi.utils.SpUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
//import com.taobao.sophix.PatchStatus;
//import com.taobao.sophix.SophixManager;
//import com.taobao.sophix.listener.PatchLoadStatusListener;

/**
 *
 * 时间：2017/7/20 16:31
 * 作用：
 */

public class App extends Application {
    private static final String TECENT_BUGLY_APPID = "eefafed14b";
    public static App instance;
    protected static Context applicationContext;
    public static SharedPreferences sp;
    public static SharedPreferences.Editor editor;
    public static Context mContext;

//    @Override
//    protected void attachBaseContext(final Context base) {
//        super.attachBaseContext(base);
//        try{
//            SophixManager.getInstance().setContext(this)
//                    .setAppVersion(getVersionName())
//                    .setAesKey(null)
//                    .setEnableDebug(true)
//                    .setPatchLoadStatusStub(new PatchLoadStatusListener() {
//                        @Override
//                        public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
//                            // 补丁加载回调通知
//                            if (code == PatchStatus.CODE_LOAD_SUCCESS) {
//                                // Toast.makeText(base, "补丁获取成功", Toast.LENGTH_SHORT).show();
//                                // 表明补丁加载成功
//                            } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
//                                // 表明新补丁生效需要重启. 开发者可提示用户或者强制重启;
//                                // 建议: 用户可以监听进入后台事件, 然后调用killProcessSafely自杀，以此加快应用补丁，详见1.3.2.3
//                                // Toast.makeText(base, "code ===1 " + code, Toast.LENGTH_SHORT).show();
//                            } else {
//                                // 其它错误信息, 查看PatchStatus类说明
//                                // Toast.makeText(base, "code === " + code, Toast.LENGTH_SHORT).show();
//                            }
//                        }
//                    }).initialize();
//
//        }catch (Exception e){
//
//        }
//    }

    @Override
    public void onCreate() {
        super.onCreate();
        applicationContext = getApplicationContext();
        instance=this;
        sp = getSharedPreferences("jiepiaisi", Context.MODE_PRIVATE);
        editor = sp.edit();
        mContext = this;
        SpUtil.init(this);
        /* 第三个参数为SDK调试模式开关，调试模式的行为特性如下：
         * 输出详细的Bugly SDK的Log；
         * 每一条Crash都会被立即上报；
         * 自定义日志将会在Logcat中输出。
         * 建议在测试阶段建议设置成true，发布时设置为false。
         * */
        //CrashReport.initCrashReport(getApplicationContext(), TECENT_BUGLY_APPID, false);
        //讯飞设置
        //SpeechUtility.createUtility(this, SpeechConstant.APPID + "=59705545");
        /**
         * 预先加载一级列表显示 全国所有城市市的数据
         */
        initJvHeCity();

    }

    private void initJvHeCity() {
        try{
            //CityListLoader.getInstance().loadCityDataFromJson2(applicationContext);
            OkGo.<String>get("http://v.juhe.cn/wz/citys?key=e48046ba7ff876ddc5fb638a76209e6c")
                    .tag(applicationContext)
                    .retryCount(3)
                    .execute(new StringCallback(){
                        @Override
                        public void onSuccess(com.lzy.okgo.model.Response<String> response) {
                            if(response.body() == null){
                                return;
                            }
                            final JvHeCityBean jvHeCityBean =  JSON.parseObject(response.body(), JvHeCityBean.class);
                            if(jvHeCityBean.resultcode.equals("200") ){
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try{
                                            if(jvHeCityBean.result == null){
                                                return;
                                            }
                                            CityListLoader.getInstance().loadCityDataFromJson3(jvHeCityBean.result);
                                        }catch (Exception e){

                                        }
                                    }
                                }).start();
                            }else {

                            }

                        }

                        @Override
                        public void onError(com.lzy.okgo.model.Response<String> response) {
                            super.onError(response);
                        }
                    });

        }catch (Exception e){

        }
    }

    /**
     * 获取全局的context
     */
    public static Context getContext() {
        return mContext;
    }

    /**
     *
     * @return
     */
    public static Context getAppContext() {
        return applicationContext;
    }

    /**
     *
     * @return
     */
    public static App getInstance() {
        return instance;
    }

    public static Resources getAppResources() {
        return getAppResources();
    }

    public static void cleanSharedpreferenceData(){
        editor.clear().commit();
    }


    public int getVersionCode() {
        //获取packagemanager的实例
        PackageManager packageManager = getPackageManager();
        //getPackageName当前类的包名，0代表是获取版本信息
        PackageInfo packageInfo = null;
        try {
            packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        int versionCode = packageInfo.versionCode;
        return versionCode;
    }

    private String getVersionName() {
        //获取packagemanager的实例
        PackageManager packageManager = getPackageManager();
        //getPackageName当前类的包名，0代表是获取版本信息
        PackageInfo packageInfo = null;
        try {
            packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String version = packageInfo.versionName;
        return version;
    }

}
